package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.service.MenuService;
import com.yhp.service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/menuservlet")
public class MenuServlet extends HttpServlet {
    MenuService menuService=new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String key = req.getParameter("key");
        switch (key){
            case "selectall":
                selectall(req, resp);
                break;
            case "delete":
                delete(req, resp);
                break;
            case "selectbyid":
                selectbyid(req, resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        String menuname = req.getParameter("mname");
        Menu menu = new Menu();
        menu.setMenuId(Integer.parseInt(mid));
        menu.setMenuName(menuname);

        int i = menuService.updateMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.print("<script type='text/javascript'>alert('修改成功');location.href='/menuservlet?key=selectall'</script>");
        }else{
            writer.print("<script type='text/javascript'>alert('修改失败');location.href='/menuservlet?key=selectbyid&rid="+mid+"'</script>");
        }

    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        int i = menuService.deleteById(Integer.parseInt(mid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.print("<script type='text/javascript'>alert('删除成功');location.href='/menuservlet?key=selectall'</script>");
        }else{
            writer.print("<script type='text/javascript'>alert('删除失败');location.href='/menuservlet?key=selectall'</script>");
        }

    }
    protected void selectbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        Menu role = menuService.findById(Integer.parseInt(mid));
        req.setAttribute("menu",role);
        req.getRequestDispatcher("/showmenu.jsp").forward(req,resp);
    }

    protected void selectall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Menu> menu = menuService.selectAll();
        req.setAttribute("menulist",menu);
        req.getRequestDispatcher("/menu.jsp").forward(req,resp);

    }

}
